<?php
namespace app\index\controller;
use app\index\model\Archives;
use app\index\model\ArchivesCategory;
use app\index\model\Collect;
use app\index\model\CollectArchivesRelation;
use app\index\model\Storeroom;
use app\index\model\Tag;
use app\index\model\TagArchivesRelation;
use app\index\model\TagGroup;
use think\Db;
use think\Request;



class Pics extends Common
{
    protected $request;
    protected $ac_model;
    protected $a_model;
    protected $tag_model;
    protected $tag_group_model;
    protected $tag_archives_relation_model;
    protected $storage_model;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->request = $request;
        $this->ac_model = new ArchivesCategory();
        $this->a_model = new Archives();
        $this->tag_model = new Tag();
        $this->tag_archives_relation_model = new TagArchivesRelation();
        $this->tag_group_model = new TagGroup();
        $this->storage_model = new Storeroom();
    }
    // 文件
    public function index()
    {
        $cate_id = $this->request->get('cate_id');
        if(empty($cate_id)) {
            // $cate_id = session('cate_id') ? session('cate_id') : '-4';
            $cate_id =  '-4';
            // session('cate_id') = '-4';
            $_SESSION['think']['cate_id'] = '-4';
        }else{
            session('cate_id','-4');
        }
        // dump($_SESSION['think']['cate_id']);die;
        $ac_data = $this->ac_model->getListByCondition(array('cate_type'=>4));
        // var_dump($ac_data);die;
        $a_data = $this->a_model->getListByCondition(array('category_id'=>$cate_id));
        // var_dump($a_data);die;
        $nav_html = '';
        $tree = '';

        // 获取用户权限
        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time = date('Y-m-d H:i:s',time());
        //记录用户操作
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];

        if ($prevdata !== '文件') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Pics/index',   
                'log_desc' => '文件',   //默认空
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $res = db('_user_log')
                ->insert($data);
        }

        $list = Db::table('cn_user')
                ->alias('cu')
                ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
                ->join('cn_character cc','cc.chara_id = cucr.chara_id')
                ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
                ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
                ->where('cn_user.user_id',$uid)
                ->field('menu_name')
                ->select();
                // var_dump(121);die;
        if($ac_data) {
            $cate_tree = recursionCate($ac_data, 'cate_id', 0, 'cate_parent_id');
            // var_dump($cate_tree);die;
            $tree = [];
            $tree = createCateTree($cate_tree, 'cate_id', 'cate_parent_id', 'cate_name');
            // var_dump($tree);die;
            if (!empty($cate_id) && $cate_id !=-2 && $cate_id !=-1 && $cate_id !=-3  && $cate_id !=-4) {
                $nav = trim(getAncestors(arrayValueToKey($ac_data, 'cate_id'), $cate_id, 0), ',');
                $nav = explode(',', $nav);
                $nav = array_reverse($nav, true);

                foreach ($nav as $k => $v) {
                    $p = explode('-', $v);
                    $nav_html .= '<li>' .
                        '<a data-id="' . $p[0] . '" href="' . url('index/Pics/index') . '">' . $p[1] . '</a>' .
                        '<i class="fa fa-angle-right"></i>' .
                        '</li>';
                }
            }
        }
        // var_dump($a_data);die;
        return view('index@Pics/index',['tree'=>$tree,'nav_html'=>$nav_html,'a_data'=>$a_data,'node'=>$list]);
    }

    //添加文件夹
    public function addFolder(){
        $this->autoCreate('folder',2);
        return view('index@Pics/addFolder');
    }
    //添加word文档
    public function addWord(){
        $this->autoCreate('word',2);
        return view('index@Pics/addWord');
    }
    //添加Excel文档
    public function addExcel(){
        $this->autoCreate('excel',2);
        return view('index@Pics/addExcel');
    }
    //添加Txt文档
    public function addTxt(){
        $this->autoCreate('txt',2);
        return view('index@Pics/addTxt');
    }
    //添加Html文档
    public function addHtml(){
        $this->autoCreate('html',2);
        return view('index@Pics/addHtml');
    }
    //添加Ppt文档
    public function addPpt(){
        $this->autoCreate('ppt',2);
        return view('index@Pics/addPpt');
    }

    //删除 编辑文件夹
    public function folderEdit(){
        $cate_id = \session('cate_id');
        if(empty($cate_id) || $cate_id == -4){
            jsonOut(0,'请先选择文件夹后存在操作');
        }
        $this->folderEdits(2);
        $cate_data = $this->ac_model->getDataByCondition(array('cate_id'=>$cate_id));
        return view('index@Pics/folderEdit',['data'=>$cate_data]);
    }

    //文件重命名
    public function rename(){
        $params = array();
        // var_dump(12);die;
        $id = $this->request->param('id',0);
        // var_dump($id);die;
        if(empty($id)){
            jsonOut(0,'参数错误');
        }
        $data = $this->a_model->getDataByCondition(array('arc_id'=>$id));
        // var_dump($data);die;
        if(empty($data)){
            jsonOut(0,'文件不存在');
        }
        $params['data'] = $data;
        // dump($params);die;
        if($this->request->instance()->isPost()) {
            $name = $this->request->post('name','');
            $describe = $this->request->post('describe','');
            if(empty($name)){
                jsonOut(0,'文件名不能为空');
            }
            //将对于的文件重命名
            $cate_id = session('cate_id');
            $path = $this->getFilePath($cate_id,4);

            if(is_file(iconv("UTF-8", "GBK", $path['path'] . '/' . $name . '.' . $data['arc_type']))){
                jsonOut(0,'文件已存在');
            }
            $oldname = iconv("UTF-8", "GBK",$path['path'] . '/' . $data['arc_name'] . '.' . $data['arc_type']);
            $newname = iconv("UTF-8", "GBK",$path['path'] . '/' . $name . '.' . $data['arc_type']);
            // $rm = rename( $oldname , $newname );
            $rm = rename(iconv("UTF-8", "GBK",$path['path'].'/'.$data['arc_name'].'.'.$data['arc_type']),iconv("UTF-8", "GBK",$path['path'].'/'.$name.'.'.$data['arc_type']));
            // var_dump($rm);die;

            if($rm) {
                $save_data = array(
                    'arc_name'=>$name,
                    'arc_desc'=>$describe,
                    'update_user_id'=>$this->admin_session['id'],
                    'update_time'=>date('Y-m-d H:i:s'),
                );
                if ($this->a_model->updateByCondition(array('arc_id' => $id), $save_data)) {
                    //操作记录
                    $this->tableBak('archives', 'archives_version', 'arc_id', 'original_id', $id, 'backup_reasons', '重命名');
                    jsonOut(1, '操作成功');
                }else{
                    jsonOut(0,'操作失败');
                }
            }else{
                jsonOut(0,'操作失败');
            }
        }
        // var_dump(777);die;

        return view('index@Pics/rename',$params);
    }
    //复制文件
    public function copy(){
        $params = array();
        $id = $this->request->param('id',0);
        if(empty($id)){
            jsonOut(0,'参数错误');
        }
        $data = $this->a_model->getDataByCondition(array('arc_id'=>$id));

        if(empty($data)){
            jsonOut(0,'文件不存在');
        }
        $params['data'] = $data;
        $ac_data = $this->ac_model->where(array('cate_type'=>2))->select();
        $cate_tree = recursionCate($ac_data, 'cate_id', 0, 'cate_parent_id');
        $tree = createCateTree($cate_tree, 'cate_id', 'cate_parent_id', 'cate_name');
        if(empty($tree)){
            jsonOut(0,'文件目录错误');
        }
        $params['tree'] = $tree;
        if($this->request->instance()->isPost()) {
            $cate_id = $this->request->param('cate_id','');
            $new_cate = $this->request->param('new_cate','');
            if(empty($cate_id) || empty($new_cate)){
                jsonOut(0,'参数错误');
            }
            $path = $this->getFilePath($cate_id,2);
            $new_path = $this->getFilePath($new_cate,2);
            $table1 = config('database.prefix').'archives';
            $new_data = Db::table($table1)->where(array('arc_id'=>$id))->find();
            $save_data = $new_data;
            unset($save_data['arc_id']);
            unset($save_data['audit_status']);
            unset($save_data['is_delete']);
            $save_data['create_user_id'] = $this->admin_session['id'];
            $save_data['update_user_id'] = $this->admin_session['id'];
            $save_data['create_time'] = date('Y-m-d H:i:s');
            $save_data['update_time'] = date('Y-m-d H:i:s');
            $save_data['category_id'] = $new_cate;
            if(is_file(iconv("UTF-8", "GBK",$new_path['path'].'/'.$data['arc_name'].'.'.$data['arc_type']))){
                jsonOut(0,'文件已存在');
            }
            $cp = copy(iconv("UTF-8", "GBK",$path['path'].'/'.$data['arc_name'].'.'.$data['arc_type']),iconv("UTF-8", "GBK",$new_path['path'].'/'.$data['arc_name'].'.'.$data['arc_type']));
            if($cp) {
                if ($this->a_model->insert($save_data)) {
                    $id = $this->a_model->getLastInsID();
                    $this->tableBak('archives', 'archives_version', 'arc_id', 'original_id', $id, 'backup_reasons', '复制');
                    jsonOut(1,'操作成功');
                }
                @unlink(iconv("UTF-8", "GBK",$new_path['path'].'/'.$data['arc_name'].'.'.$data['arc_type']));
                jsonOut(0,'操作失败');

            }else{
                jsonOut(0,'操作失败');
            }
        }
        return view('index@Pics/copy',$params);
    }
    //移动文件
    public function remove(){
        $params = array();
        $id = $this->request->param('id',0);
        if(empty($id)){
            jsonOut(0,'参数错误');
        }
        $data = $this->a_model->getDataByCondition(array('arc_id'=>$id));

        if(empty($data)){
            jsonOut(0,'文件不存在');
        }
        $params['data'] = $data;
        $ac_data = $this->ac_model->where(array('cate_type'=>2))->select();
        $cate_tree = recursionCate($ac_data, 'cate_id', 0, 'cate_parent_id');
        $tree = createCateTree($cate_tree, 'cate_id', 'cate_parent_id', 'cate_name');
        if(empty($tree)){
            jsonOut(0,'文件目录错误');
        }
        $params['tree'] = $tree;
        if($this->request->instance()->isPost()) {
            $cate_id = $this->request->param('cate_id','');
            $new_cate = $this->request->param('new_cate','');
            if(empty($cate_id) || empty($new_cate)){
                jsonOut(0,'参数错误');
            }
            $path = $this->getFilePath($cate_id,2);
            $new_path = $this->getFilePath($new_cate,2);
            $save_data['category_id'] = $new_cate;
            if(is_file(iconv("UTF-8", "GBK",$new_path['path'].'/'.$data['arc_name'].'.'.$data['arc_type']))){
                jsonOut(0,'文件已存在');
            }
            $cp = copy(iconv("UTF-8", "GBK",$path['path'].'/'.$data['arc_name'].'.'.$data['arc_type']),iconv("UTF-8", "GBK",$new_path['path'].'/'.$data['arc_name'].'.'.$data['arc_type']));
            if($cp) {
                if($this->a_model->updateByCondition(array('arc_id'=>$id),array('category_id'=>$new_cate))) {
                    @unlink(iconv("UTF-8", "GBK", $path['path'] . '/' . $data['arc_name'] . '.' . $data['arc_type']));
                    $this->tableBak('archives', 'archives_version', 'arc_id', 'original_id', $id, 'backup_reasons', '移动');
                    jsonOut(1, '操作成功');
                }else{
                    jsonOut(0,'操作失败');
                }
            }else{
                jsonOut(0,'操作失败');
            }
        }

        return view('index@Pics/remove',$params);
    }
    //删除文件
    public function delete(){
        $params = array();
        $id = $this->request->param('id',0);
        if(empty($id)){
            jsonOut(0,'参数错误');
        }
        $data = $this->a_model->getDataByCondition(array('arc_id'=>$id));

        if(empty($data)){
            jsonOut(0,'文件不存在');
        }
        $params['data'] = $data;
        if($this->request->instance()->isPost()) {

            if ($this->a_model->updateByCondition(array('arc_id' => $id), array('is_delete' => 1))) {
                $this->tableBak('archives', 'archives_version', 'arc_id', 'original_id', $id, 'backup_reasons', '删除');
                jsonOut(1, '删除成功');
            }else{
                jsonOut(1, '删除失败');
            }
            /*****************移动并删除的逻辑*******************/
            //            $path = $this->getFilePath(session('cate_id'));
            /*$cp = copy(iconv("UTF-8", "GBK",$path['path'].'/'.$data['arc_name'].'.'.$data['arc_type']),iconv("UTF-8", "GBK",$this->folderPath.'/delete_all/'.$data['arc_name'].'.'.$data['arc_type']));
            if($cp) {
                if ($this->a_model->updateByCondition(array('arc_id' => $id), array('is_delete' => 1))) {
                    @unlink(iconv("UTF-8", "GBK", $path['path'] . '/' . $data['arc_name'] . '.' . $data['arc_type']));
                    jsonOut(1, '删除成功');
                }else{
                    @unlink(iconv("UTF-8", "GBK", $this->folderPath.'/delete_all/' . $data['arc_name'] . '.' . $data['arc_type']));
                    jsonOut(1, '删除失败');
                }
            }else{
                @unlink(iconv("UTF-8", "GBK", $this->folderPath.'/delete_all/' . $data['arc_name'] . '.' . $data['arc_type']));
                jsonOut(1, '删除失败');
            }*/
        }
        return view('index@Pics/delete',$params);
    }

    //收藏文件
    public function shouc(){
        $params = array();
        $id = $this->request->param('id',0);
        if(empty($id)){
            jsonOut(0,'参数错误');
        }
        $data = $this->a_model->getDataByCondition(array('arc_id'=>$id));

        if(empty($data)){
            jsonOut(0,'文件不存在');
        }
        $params['data'] = $data;
        if($this->request->instance()->isPost()) {
            //先创建 收藏表数据
            $collect = new Collect();
            $collect_arc = new CollectArchivesRelation();
            //先查询用户是否已经收藏过
            $f_data = $collect_arc->getDataByCondition(array('arc_id'=>$id,'create_user_id'=>$this->admin_session['id']));
            if($f_data){
                jsonOut(0,'您已经收藏过了');
            }
            $c_data = array(
                'create_user_id'=>$this->admin_session['id'],
                'create_time'=>date('Y-m-d H:i:s'),
                'update_user_id'=>$this->admin_session['id'],
                'update_time'=>date('Y-m-d H:i:s')
            );
            if($collect->insert($c_data)){
                //插入收藏关联表
                $ca_data = array(
                    'arc_id'=>$id,
                    'col_id'=>$collect->getLastInsID(),
                    'create_user_id'=>$this->admin_session['id'],
                    'create_time'=>date('Y-m-d H:i:s'),
                    'update_user_id'=>$this->admin_session['id'],
                    'update_time'=>date('Y-m-d H:i:s')
                );
                if($collect_arc->insert($ca_data)){
                    jsonOut(1,'收藏成功');
                }else{
                    jsonOut(0,'收藏失败');
                }
            }
        }
        return view('index@Prearc/shouc',$params);
    }

    //下载原文件
    public function download(){
        $id = $this->request->param('id','');
        if(empty($id)){
            jsonOut(0,'参数错误');
        }
        $this->toDownload($id);
    }
    //指定标签
    public function settag(){
        $params = array();
        $id = $this->request->param('id',0);
        if(empty($id)){
            jsonOut(0,'参数错误');
        }
        $data = $this->a_model->getDataByCondition(array('arc_id'=>$id));

        if(empty($data)){
            jsonOut(0,'文件不存在');
        }
        $params['data'] = $data;
        return view('index@Pics/settag',$params);
    }
    //设置标签
    public function settags(){
        $params = array();
        //取标签组
        $tag_group_data = $this->tag_group_model->getListByCondition(array(),'tag_group_sort desc');
        $params['tgd'] = $tag_group_data;
        //先去第组下面的对应的标签的第一个显示为上级标签
        $tag_data = $this->tag_model->getListByCondition(array('tag_group_id'=>$tag_group_data[0]['tag_gruop_id'],'tag_parent_id'=>0),'tag_sort desc');
        $params['tds'] = $tag_data;
        if($this->request->instance()->isPost()){
            $tag_val = $this->request->post('tag_val','');//1添加标签组 2添加标签
            if(empty($tag_val)){
                jsonOut(0,'参数错误');
            }
            if($tag_val == 1){
                $tag = $this->request->post('tag','');
                $ptag = $this->request->post('ptag',0);
                $gtag = $this->request->post('gtag',0);
                $otag = $this->request->post('otag',99);
                if(empty($tag)){
                    jsonOut(0,'添加信息不全');
                }
                $save_data = array(
                    'tag_name'=>$tag,
                    'tag_sort'=>$otag,
                    'tag_parent_id'=>$ptag,
                    'tag_group_id'=>$gtag,
                    'create_user_id'=>$this->admin_session['id'],
                    'create_time'=>date('Y-m-d H:i:s'),
                    'update_user_id'=>$this->admin_session['id'],
                    'update_time'=>date('Y-m-d H:i:s')
                );
                if($this->tag_model->insert($save_data)){
                    jsonOut(1,'添加标签成功');
                }else{
                    jsonOut(0,'添加失败');
                }
            }else{
                $tags = $this->request->post('tags','');
                $otags = $this->request->post('otags',99);
                $is_only = $this->request->post('is_only',0);
                $is_required = $this->request->post('is_required',0);
                if(empty($tags)){
                    jsonOut(0,'标签组名称错误');
                }
                $save_data = array(
                    'tag_group_name'=>$tags,
                    'tag_group_sort'=>$otags,
                    'is_only'=>$is_only,
                    'is_required'=>$is_required,
                    'create_user_id'=>$this->admin_session['id'],
                    'create_time'=>date('Y-m-d H:i:s'),
                    'update_user_id'=>$this->admin_session['id'],
                    'update_time'=>date('Y-m-d H:i:s')
                );
                if($this->tag_group_model->insert($save_data)){
                    jsonOut(1,'添加标签组成功');
                }else{
                    jsonOut(0,'添加失败');
                }
            }
        }
        return view('index@Pics/settags',$params);
    }
    //获取标签
    public function getTags(){
        $id = $this->request->post('id','');
        if(empty($id)){
            jsonOut(0,'参数错误');
        }
        $data = $this->tag_model->getListByCondition(array('tag_group_id'=>$id,'tag_parent_id'=>0),'tag_sort desc');
        if(empty($data)){
            jsonOut(0,'暂无数据');
        }else{
            jsonOut(1,'查询成功',$data);
        }
    }
    //修改标签组
    public function updateTags(){
        if($this->request->instance()->isPost()){
            $id = $this->request->post('tagsid','');
            $tags_name = $this->request->post('tags','');
            $otags = $this->request->post('otags',99);
            $is_only = $this->request->post('is_only',0);
            $is_required = $this->request->post('is_required',0);
            if(empty($id) || empty($tags_name)){
                jsonOut(0,'名称不能为空');
            }
            $save_data = array(
                'tag_group_name'=>$tags_name,
                'tag_group_sort'=>$otags,
                'is_only'=>$is_only,
                'is_required'=>$is_required,
                'update_user_id'=>$this->admin_session['id'],
                'update_time'=>date('Y-m-d H:i:s')
            );
            if($this->tag_group_model->updateByCondition(array('tag_gruop_id'=>$id),$save_data)){
                jsonOut(1,'编辑成功');
            }else{
                jsonOut(0,'编辑失败');
            }
        }
    }
    //修改标签
    public function updateTag(){
        if($this->request->instance()->isPost()){
            $id = $this->request->post('tagid','');
            $tag_name = $this->request->post('tag','');
            $otag = $this->request->post('otag',99);
            if(empty($id) || empty($tag_name)){
                jsonOut(0,'名称不能为空');
            }
            $save_data = array(
                'tag_name'=>$tag_name,
                'tag_sort'=>$otag,
                'update_user_id'=>$this->admin_session['id'],
                'update_time'=>date('Y-m-d H:i:s')
            );
            if($this->tag_model->updateByCondition(array('tag_id'=>$id),$save_data)){
                jsonOut(1,'编辑成功');
            }else{
                jsonOut(0,'编辑失败');
            }
        }
    }
    //删除标签或者标签组
    public function deleteTag(){
        if($this->request->instance()->isPost()){
            $tag_type = $this->request->post('tag_type','');
            $tag_val = $this->request->post('tag_val','');
            if(empty($tag_val) || empty($tag_type)){
                jsonOut(0,'参数错误');
            }
            if($tag_type == 1){
                $tag_group_data = $this->tag_group_model->getDataByCondition(array('tag_gruop_id'=>$tag_val));
                if(empty($tag_group_data)){
                    jsonOut(0,'暂无数据');
                }
                //查询下面是否存在未删除的标签
                $f_tag = $this->tag_model->getDataByCondition(array('tag_group_id'=>$tag_val,'is_delete'=>0));
                if(empty($f_tag)){
                    //下面没有未删除的标签 可以删除
                    if($this->tag_group_model->updateByCondition(array('tag_gruop_id'=>$tag_val),array('is_delete'=>1))){
                        jsonOut(1,'标签组删除成功');
                    }else{
                        jsonOut(0,'标签组删除失败');
                    }
                }else{
                    jsonOut(0,'标签组下存在未删除的标签，请先删除标签后重试');
                }
            }else{
                $tag_data = $this->tag_model->getDataByCondition(array('tag_id'=>$tag_val));
                if(empty($tag_data)){
                    jsonOut(0,'暂无数据');
                }
                if($tag_data['tag_parent_id'] ==0){
                    //一级标签 先看下面有没有没有删除的标签
                    $f_tag_data = $this->tag_model->getDataByCondition(array('tag_parent_id'=>$tag_val,'is_delete'=>0));
                    if(empty($f_tag_data)){
                        //下面没有未删除的标签 可以删除
                        if($this->tag_model->updateByCondition(array('tag_id'=>$tag_val),array('is_delete'=>1))){
                            jsonOut(1,'删除标签成功');
                        }else{
                            jsonOut(0,'删除标签失败');
                        }
                    }else{
                        jsonOut(0,'标签存在下级标签，请先删除下级标签后重试');
                    }
                }else{
                    //二级标签 直接删除
                    if($this->tag_model->updateByCondition(array('tag_id'=>$tag_val),array('is_delete'=>1))){
                        jsonOut(1,'删除标签成功');
                    }else{
                        jsonOut(0,'删除标签失败');
                    }
                }
            }
        }
    }
    //获取标签树结构
    public function getTagsTree(){
        $tags = $this->tag_group_model->getListByCondition(array(),'tag_group_sort desc');
        $html = '';
        if($tags) {
            foreach ($tags as $key => $val) {
                if($val['is_only'] == 1){
                    $is_only='（单选）';
                }else{
                    $is_only='（多选）';
                }
                $html .= '<tr>'.
                    '<td class="highlight">'.
                    '<div class="success"></div>'.
                    '<a href="javascript:;">'.$val['tag_group_name'].$is_only.'</a>'.
                    '</td>'.
                    '<td>'.
                    '</td>'.
                    '<td>'.
                    '</td>'.
                    '<td>'.
                    '<a href="javascript:;" class="btn default btn-xs purple editBtn" data-tagname="'.$val['tag_group_name'].'" data-order="'.$val['tag_group_sort'].'" data-only="'.$val['is_only'].'" data-required="'.$val['is_required'].'" data-type="1" data-id="'.$val['tag_gruop_id'].'">'.
                    '<i class="fa fa-edit"></i> 编辑'.
                    '</a>'.
                    '<a href="javascript:;" class="btn default btn-xs black delBtn" data-tagname="'.$val['tag_group_name'].'" data-type="1" data-id="'.$val['tag_gruop_id'].'">'.
                    '<i class="fa fa-trash-o"></i> 删除'.
                    '</a>'.
                    '</td>'.
                    '</tr>';
                $otag = $this->tag_model->getListByCondition(array('tag_group_id' => $val['tag_gruop_id'], 'tag_parent_id' => 0), 'tag_sort desc');
                if($otag){
                    foreach ($otag as $k=>$v) {
                        $html .= '<tr>'.
                            '<td class="highlight">'.
                            '<div class="info"></div>|--'.
                            '</td>'.
                            '<td>'.
                            '<a href="javascript:;">'.$v['tag_name'].'</a>'.
                            '</td>'.
                            '<td>'.
                            '</td>'.
                            '<td>'.
                            '<a href="javascript:;" class="btn default btn-xs purple editBtn" data-tagname="'.$v['tag_name'].'" data-order="'.$v['tag_sort'].'" data-type="2"  data-id="'.$v['tag_id'].'">'.
                            '<i class="fa fa-edit"></i> 编辑'.
                            '</a>'.
                            '<a href="javascript:;" class="btn default btn-xs black delBtn" data-tagname="'.$v['tag_name'].'"  data-type="2"  data-id="'.$v['tag_id'].'">'.
                            '<i class="fa fa-trash-o"></i> 删除'.
                            '</a>'.
                            '</td>'.
                            '</tr>';
                        $ttag = $this->tag_model->getListByCondition(array('tag_group_id' => $val['tag_gruop_id'], 'tag_parent_id' => $v['tag_id']), 'tag_sort desc');
                        if($ttag) {
                            foreach ($ttag as $k1 => $v1) {
                                $html .= '<tr>'.
                                    '<td class="highlight">'.
                                    '<div class="warning"></div>'.
                                    '</td>'.
                                    '<td>|--'.
                                    '</td>'.
                                    '<td>'.
                                    '<a href="javascript:;">'.$v1['tag_name'].'</a>'.
                                    '</td>'.
                                    '<td>'.
                                    '<a href="javascript:;" class="btn default btn-xs purple editBtn"  data-tagname="'.$v1['tag_name'].'" data-order="'.$v1['tag_sort'].'"  data-type="2"  data-id="'.$v1['tag_id'].'">'.
                                    '<i class="fa fa-edit"></i> 编辑'.
                                    '</a>'.
                                    '<a href="javascript:;" class="btn default btn-xs black delBtn" data-tagname="'.$v1['tag_name'].'" data-type="2"  data-id="'.$v1['tag_id'].'">'.
                                    '<i class="fa fa-trash-o"></i> 删除'.
                                    '</a>'.
                                    '</td>'.
                                    '</tr>';
                            }
                        }
                    }
                }

            }
        }
        jsonOut(1,'查询成功',$html);
    }
    //获取标签树结构
    public function getTagsTree1(){
        $tags = $this->tag_group_model->getListByCondition(array(),'tag_group_sort desc');
        $html = '';
        if($tags) {
            foreach ($tags as $key => $val) {
                if($val['is_only'] == 1){
                    $is_only='（单选）';
                }else{
                    $is_only='（多选）';
                }
                $html .= '<tr>'.
                    '<td class="highlight">'.
                    '<div class="success"></div>'.
                    '<a href="javascript:;">'.$val['tag_group_name'].$is_only.'</a>'.
                    '</td>'.
                    '<td>'.
                    '</td>'.
                    '<td>'.
                    '</td>'.
                    '<td>'.
                    '</td>'.
                    '</tr>';
                $otag = $this->tag_model->getListByCondition(array('tag_group_id' => $val['tag_gruop_id'], 'tag_parent_id' => 0), 'tag_sort desc');
                if($otag){
                    foreach ($otag as $k=>$v) {
                        $html .= '<tr>'.
                            '<td class="highlight">'.
                            '<div class="info"></div>|--'.
                            '</td>'.
                            '<td>'.
                            '<a href="javascript:;">'.$v['tag_name'].'</a>'.
                            '</td>'.
                            '<td>'.
                            '</td>'.
                            '<td>'.
                            '<a href="javascript:;" class="btn default btn-xs blue editBtn" data-tagname="'.$v['tag_name'].'" data-order="'.$v['tag_sort'].'" data-type="2"  data-id="'.$v['tag_id'].'">'.
                            '<i class="fa fa-check-square"></i> 设置标签'.
                            '</a>'.
                            '</td>'.
                            '</tr>';
                        $ttag = $this->tag_model->getListByCondition(array('tag_group_id' => $val['tag_gruop_id'], 'tag_parent_id' => $v['tag_id']), 'tag_sort desc');
                        if($ttag) {
                            foreach ($ttag as $k1 => $v1) {
                                $html .= '<tr>'.
                                    '<td class="highlight">'.
                                    '<div class="warning"></div>'.
                                    '</td>'.
                                    '<td>|--'.
                                    '</td>'.
                                    '<td>'.
                                    '<a href="javascript:;">'.$v1['tag_name'].'</a>'.
                                    '</td>'.
                                    '<td>'.
                                    '<a href="javascript:;" class="btn default btn-xs blue editBtn"  data-tagname="'.$v1['tag_name'].'" data-order="'.$v1['tag_sort'].'"  data-type="2"  data-id="'.$v1['tag_id'].'">'.
                                    '<i class="fa fa-check-square"></i> 设置标签'.
                                    '</a>'.
                                    '</td>'.
                                    '</tr>';
                            }
                        }
                    }
                }

            }
        }
        jsonOut(1,'查询成功',$html);
    }
    //设置cate_id
    public function setCateId(){
        $cate_id = $this->request->post('cate_id',0);
        session('cate_id',$cate_id);
        jsonOut(1,'设置成功');

    }
    //获取标签数据
    public function getTagsData(){
        if($this->request->instance()->isPost()) {
            $id = $this->request->post('id', '');
            if (empty($id)) {
                jsonOut(0, '参数错误');
            }
            //查找文件是否有标签
            $tag_rel_data = $this->getTagsByArcId($id);
            if (empty($tag_rel_data)) {
                jsonOut(1, '', '暂无标签');
            } else {
                $html = '';
                foreach ($tag_rel_data as $key => $val) {
                    if ($val['tag_info']) {
                        $html .= '<a href="#" data-id="' . $val['rel_id'] . '" class="btn btn-small default delTagBtn" style="padding: 2px 2px 2px 2px;margin: 4px 2px 4px 2px">' . $val['tag_info']['tag_name'] .
                            ' <i class="fa fa-close"></i>' .
                            '</a>';
                    }
                }
                jsonOut(1, '', $html);
            }
        }
    }
    //设置标签数据
    public function setTagData(){
        if($this->request->instance()->isPost()) {
            $id = $this->request->post('id', '');
            $tid = $this->request->post('tid', '');
            if(empty($id) || empty($tid)){
                jsonOut(0,'参数错误');
            }
            //查询设置的标签是否存在
            $f_data = $this->tag_archives_relation_model->getDataByCondition(array('tag_id'=>$tid,'arc_id'=>$id));
            if(empty($f_data)) {
                $save_data = array(
                    'tag_id' => $tid,
                    'arc_id' => $id,
                    'create_user_id' => $this->admin_session['id'],
                    'create_time' => date('Y-m-d H:i:s'),
                    'update_user_id' => $this->admin_session['id'],
                    'update_time' => date('Y-m-d H:i:s')
                );
                if ($this->tag_archives_relation_model->insert($save_data)) {
                    jsonOut(1, '设置成功');
                } else {
                    jsonOut(0, '设置失败');
                }
            }else{
                jsonOut(0,'标签已存在');
            }
        }
    }
    //删除标签
    public function delTagData(){
        if($this->request->instance()->isPost()) {
            $id = $this->request->post('id', '');
            if(empty($id)){
                jsonOut(0,'参数错误');
            }
            $save_data = array(
                'is_delete'=>1,
                'update_user_id' => $this->admin_session['id'],
                'update_time' => date('Y-m-d H:i:s')
            );
            if($this->tag_archives_relation_model->updateByCondition(array('rel_id'=>$id),$save_data)){
                jsonOut(1,'删除成功');
            }else{
                jsonOut(0,'删除失败');
            }
        }
    }
    //在线阅读
    public function onRead(){
        $params = array();
        $id = $this->request->param('id',0);
        if(empty($id)){
            jsonOut(0,'参数错误');
        }
        $data = $this->a_model->getDataByCondition(array('arc_id'=>$id));
        if(empty($data)){
            jsonOut(0,'文件不存在');
        }

        $filePath = $this->getFilePathUrl($data['category_id'],4);
        // var_dump($filePath);die;
        $filePath = $filePath['path'].'/'.$data['arc_name'].'.'.$data['arc_type'];
        $data['filePath'] = $filePath;
        $data['http'] = 'http://'.$_SERVER['HTTP_HOST'];
        $data['box_path'] = $this->storage_model->getStoragePath($data['box_id']);
        $data['arc_time'] = date('Y-m-d',strtotime($data['arc_time']));
        $params['data'] = $data;

        $ac_data = $this->storage_model->where(array('is_delete'=>0))->select();
        $cate_tree = recursionStorage($ac_data, 'sto_id', 0, 'patent_id');
        $tree = createStorageTree($cate_tree, 'sto_id', 'patent_id', 'sto_name');
        if(empty($tree)){
            jsonOut(0,'文件目录错误');
        }
        $params['tree'] = $tree;
        $params['filePath'] = $filePath;
        $params['type'] = $data['arc_type'];
        return view('index@Pics/onRead',$params);
    }



    //编辑文档信息
    public function setArc(){
        if($this->request->instance()->isPost()) {
            $id = $this->request->post('id');
            var_dump($id);die;
            $time = $this->request->post('time');
            $arc_class_num = $this->request->post('arc_class_num');
            $arc_file_num = $this->request->post('arc_file_num');
            $arc_total_num = $this->request->post('arc_total_num');
            $arc_part_num = $this->request->post('arc_part_num');
            $arc_num = $this->request->post('arc_num');
            $arc_establishment_organization  = $this->request->post('arc_establishment_organization');
            $arc_year = $this->request->post('arc_year');
            $arc_rank = $this->request->post('arc_rank');
            $arc_entities_num = $this->request->post('arc_entities_num');
            $arc_borrowed = $this->request->post('arc_borrowed');
            $arc_page_num = $this->request->post('arc_page_num');
            $box_id = $this->request->post('box_id');
            $arc_storage_position = $this->request->post('arc_storage_position');
            $arc_remarks = $this->request->post('arc_remarks');
            $arc_time = $this->request->post('arc_time');
            $arc_time_limit = $this->request->post('arc_time_limit');
            $data = $this->a_model->getDataByCondition(array('arc_id'=>$id));
            if(empty($data)){
                jsonOut(0,'档案信息错误');
            }
            //先查询档案盒里面是否已经满了
            $h_data = $this->storage_model->getDataByCondition(array('sto_id'=>$box_id));
            if(empty($h_data)){
                jsonOut(0,'档案盒不能为空');
            }
            $h_sy = $h_data['sto_available_pages']-$h_data['to_available_pages'];
            if($arc_page_num > $h_sy){
                jsonOut(0,'档案盒最大还可以存放'.$h_sy.'页数据');
            }
            $save_data = array(
                'arc_reference'=>$arc_reference,
                'arc_class_num'=>$arc_class_num,
                'arc_file_num'=>$arc_file_num,
                'arc_total_num'=>$arc_total_num,
                'arc_part_num'=>$arc_part_num,
                'arc_num'=>$arc_num,
                'arc_establishment_organization'=>$arc_establishment_organization,
                'arc_year'=>$arc_year,
                'arc_rank'=>$arc_rank,
                'arc_entities_num'=>$arc_entities_num,
                'arc_borrowed'=>$arc_borrowed,
                'box_id'=>$box_id,
                'arc_storage_position'=>$arc_storage_position,
                'arc_remarks'=>$arc_remarks,
                'arc_time'=>$arc_time." 00:00:00",
                'arc_time_limit'=>$arc_time_limit,
                'arc_page_num'=>$arc_page_num
            );
            if($this->a_model->updateByCondition(array('arc_id'=>$id),$save_data)){
                //先把之前的档案盒页数开放
                $old_sto_data = $this->storage_model->getDataByCondition(array('sto_id'=>$data['box_id']));
                $this->storage_model->updateByCondition(array('sto_id'=>$data['box_id']),array('to_available_pages'=>$old_sto_data['to_available_pages']-$arc_page_num));
                //在把对应的档案盒页数减少
                $this->storage_model->updateByCondition(array('sto_id'=>$box_id),array('to_available_pages'=>$h_data['to_available_pages']+$arc_page_num));
                jsonOut(1,'文档信息编辑成功');
            }else{
                jsonOut(0,'文档信息编辑失败');
            }
        }
    }
    //上传文件
    public function upload(){
        // session('cate_id','mark');
        $params = array();
        return view('index@Pics/upload',$params);
    }
    //文件上传
    public function uploadFile()
    {
        // var_dump($_SESSION['cate_id']);die;
        parent::uploadFile(); // TODO: Change the autogenerated stub
    }
    
}
